home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / CocktailManager / CoctailManager.jar / CoctailBar.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-09-07  |  3.7 KB  |  117 lines

  1. import java.util.Enumeration;
  2. import java.util.Hashtable;
  3. import java.util.Random;
  4. import java.util.Vector;
  5.  
  6. public class CoctailBar {
  7.    Hashtable coctails;
  8.    Random random;
  9.  
  10.    public CoctailBar() {
  11.       this(10);
  12.    }
  13.  
  14.    public CoctailBar(int barSize) {
  15.       this.coctails = new Hashtable(barSize);
  16.       this.random = new Random();
  17.    }
  18.  
  19.    public void addCoctail(Coctail coctail) {
  20.       this.coctails.put(coctail.getName(), coctail);
  21.    }
  22.  
  23.    public void addCoctail(String name, String ingredients, String recipe) {
  24.       Coctail c = new Coctail(name, ingredients, recipe);
  25.       this.coctails.put(name, c);
  26.    }
  27.  
  28.    public Coctail getCoctail(String name) {
  29.       name = name.toUpperCase();
  30.       Enumeration enum = this.coctails.keys();
  31.       String coctail = null;
  32.  
  33.       while(enum.hasMoreElements()) {
  34.          coctail = (String)enum.nextElement();
  35.          if (name.equals(coctail.toUpperCase())) {
  36.             break;
  37.          }
  38.       }
  39.  
  40.       return (Coctail)this.coctails.get(coctail);
  41.    }
  42.  
  43.    public Coctail getRandomCoctail() {
  44.       Enumeration enum = this.coctails.elements();
  45.       int i = Math.abs(this.random.nextInt() % this.coctails.size());
  46.       int j = 0;
  47.  
  48.       Coctail coctail;
  49.       do {
  50.          coctail = (Coctail)enum.nextElement();
  51.       } while(j++ < i && enum.hasMoreElements());
  52.  
  53.       return coctail;
  54.    }
  55.  
  56.    public Coctail setScore(String coctailName, int score) {
  57.       Coctail coctail = this.getCoctail(coctailName);
  58.       coctail.setScore(score);
  59.       return coctail;
  60.    }
  61.  
  62.    public String[] searchCoctail(String byName, String byIngredients) {
  63.       Vector coctails = null;
  64.       if (byName.length() != 0) {
  65.          coctails = this.searchCoctailByName(byName.toUpperCase());
  66.       } else if (byIngredients.length() != 0) {
  67.          coctails = this.searchCoctailByIngredients(byIngredients.toUpperCase());
  68.       }
  69.  
  70.       String[] coctailList = new String[coctails.size()];
  71.       coctails.copyInto(coctailList);
  72.       return coctailList;
  73.    }
  74.  
  75.    private Vector searchCoctailByName(String name) {
  76.       Enumeration enum = this.coctails.keys();
  77.       String key = null;
  78.       Vector coctailList = new Vector();
  79.  
  80.       while(enum.hasMoreElements()) {
  81.          key = (String)enum.nextElement();
  82.          key = key.toUpperCase();
  83.          if (key.indexOf(name) != -1) {
  84.             coctailList.addElement(key);
  85.          }
  86.       }
  87.  
  88.       return coctailList;
  89.    }
  90.  
  91.    private Vector searchCoctailByIngredients(String ingredients) {
  92.       Enumeration enum = this.coctails.elements();
  93.       String key = null;
  94.       Vector coctailList = new Vector();
  95.  
  96.       while(enum.hasMoreElements()) {
  97.          Coctail coctail = (Coctail)enum.nextElement();
  98.          String coctailIngredients = coctail.getIngredients().toUpperCase();
  99.          if (coctailIngredients.indexOf(ingredients) != -1) {
  100.             coctailList.addElement(coctail.getName());
  101.          }
  102.       }
  103.  
  104.       return coctailList;
  105.    }
  106.  
  107.    public String[] getCoctailList() {
  108.       Enumeration enum = this.coctails.keys();
  109.       String[] coctailList = new String[this.coctails.size()];
  110.  
  111.       for(int i = 0; enum.hasMoreElements(); coctailList[i++] = (String)enum.nextElement()) {
  112.       }
  113.  
  114.       return coctailList;
  115.    }
  116. }
  117.